package com.ruoyi.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HubUtil {
    public static String doPostWithJson(String url,String json,String token){
        String returnValue = null;
        CloseableHttpClient httpclient = null;
        new HttpHead();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            //创建httpclient对象
            httpclient = HttpClients.createDefault();
            //创建http POST请求
            HttpPost httpPost = new HttpPost(url);
            StringEntity requestEntity = new StringEntity(json,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type","application/json");
            httpPost.setHeader("Authorization","Bearer "+token);
            httpPost.setEntity(requestEntity);
            //发送请求，获取返回值
            returnValue = httpclient.execute(httpPost,responseHandler);

        }catch (Exception e) {

        }finally {
            try {
                httpclient.close();
            } catch (IOException e) {

            }
        }
        return returnValue;
    }
    public static String doGetWithJson(String url,String json,String token){
        String returnValue = null;
        CloseableHttpClient httpclient = null;
        new HttpHead();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            //创建httpclient对象
            httpclient = HttpClients.createDefault();
            //创建http get请求
            HttpGet httpGet = new HttpGet(url);
            StringEntity requestEntity = new StringEntity(json,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpGet.setHeader("Content-type","application/json");
            httpGet.setHeader("Authorization","Bearer "+token);
//            httpGet.setEntity(requestEntity);
            //发送请求，获取返回值
            returnValue = httpclient.execute(httpGet,responseHandler);

        }catch (Exception e) {

        }finally {
            try {
                httpclient.close();
            } catch (IOException e) {

            }
        }
        return returnValue;
    }
    /**
     * 测试get请求方式
     *
     * @param apiUrl 链接
     * @return 请求返回值
     * @author 明快de玄米61
     * @date 2021/09/05
     */
    public static String doGetWithJson(String apiUrl) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            // 创建客户端连接对象
            client = HttpClients.createDefault();
            // 构建Get请求对象
            HttpGet get = new HttpGet(apiUrl);
            // 设置超时时间，其中connectionRequestTimout（从连接池获取连接的超时时间）、connetionTimeout（客户端和服务器建立连接的超时时间）、socketTimeout（客户端从服务器读取数据的超时时间），单位都是毫秒
            RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(3000)
                    .setSocketTimeout(20000).build();
            get.setConfig(config);
            // 获取返回对象
            response = client.execute(get);
            // 整理返回值
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭连接和流
            try {
                if (client != null) {
                    client.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
